package com.mall.order.listener;

import com.mall.common.to.mq.SeckillOrderTo;
import com.mall.order.entity.OrderEntity;
import com.mall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void Listener(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws IOException {
        try {
            System.out.println("准备创建秒杀单的详细信息。。。");
            orderService.createSeckillOrder(seckillOrderTo);
            // 手动调用支付宝收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

}
